package com.escape.auth.application.controller;

import com.alibaba.fastjson.JSON;
import com.escape.auth.application.convert.AuthPermissionDTOConverter;
import com.escape.auth.application.dto.AuthPermissionDTO;
import com.escape.auth.common.entity.Result;
import com.escape.auth.domain.entity.AuthPermissionBO;
import com.escape.auth.domain.service.AuthPermissionDomainService;
import com.google.common.base.Preconditions;
import com.sun.org.apache.xpath.internal.operations.Bool;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @Author: escape_zebra
 * @CreateTime: 2025-01-10  16:00
 * @Description: TODO
 * @Version: 1.0
 */
@RestController
@Slf4j
@RequestMapping("/permission/")
public class PermissionController {

    @Resource
    private AuthPermissionDomainService authPermissionDomainService;

    /**
     * 新增权限
     */
    @RequestMapping("add")
    public Result<Boolean> add(@RequestBody AuthPermissionDTO authPermissionDTO){
        try {
            if (log.isInfoEnabled()) {
                log.info("PermissionController.add.DTO: {}", JSON.toJSONString(authPermissionDTO));
            }
            Preconditions.checkArgument(!StringUtils.isBlank(authPermissionDTO.getName()), "权限名不能为空!");
            Preconditions.checkArgument(!StringUtils.isBlank(authPermissionDTO.getIcon()), "图标不能为空!");
            Preconditions.checkArgument(!StringUtils.isBlank(authPermissionDTO.getPermissionKey()), "key 不能为空!");
            Preconditions.checkArgument(!StringUtils.isBlank(authPermissionDTO.getMenuUrl()), "菜单图标不能为空!");
            Preconditions.checkNotNull(authPermissionDTO.getParentId(), "父 id 不能为空!");
            Preconditions.checkNotNull(authPermissionDTO.getType(), "type 不能为空!");

            AuthPermissionBO authPermissionBO = AuthPermissionDTOConverter.INSTANCE.convertDtoToBo(authPermissionDTO);

            return Result.ok(this.authPermissionDomainService.add(authPermissionBO));
        } catch (Exception e) {
            log.error("PermissionController.add.error: {}", e.getMessage(), e);
            return Result.fail("新增权限失败!");
        }
    }

    /**
     * 更新权限
     */
    @RequestMapping("update")
    public Result<Boolean> update(@RequestBody AuthPermissionDTO authPermissionDTO){
        try {
            if (log.isInfoEnabled()) {
                log.info("PermissionController.update.DTO: {}", JSON.toJSONString(authPermissionDTO));
            }
            Preconditions.checkNotNull(authPermissionDTO.getId(), "id 不能为空!");

            AuthPermissionBO authPermissionBO = AuthPermissionDTOConverter.INSTANCE.convertDtoToBo(authPermissionDTO);

            return Result.ok(this.authPermissionDomainService.update(authPermissionBO));
        } catch (Exception e) {
            log.error("PermissionController.update.error: {}", e.getMessage(), e);
            return Result.fail("更新权限失败!");
        }
    }

    /**
     * 删除权限
     */
    @RequestMapping("delete")
    public Result<Boolean> delete(@RequestBody AuthPermissionDTO authPermissionDTO){
        try {
            if (log.isInfoEnabled()) {
                log.info("PermissionController.delete.DTO: {}", JSON.toJSONString(authPermissionDTO));
            }
            Preconditions.checkNotNull(authPermissionDTO.getId(), "id 不能为空!");

            AuthPermissionBO authPermissionBO = AuthPermissionDTOConverter.INSTANCE.convertDtoToBo(authPermissionDTO);
            return Result.ok(this.authPermissionDomainService.delete(authPermissionBO));
        } catch (Exception e) {
            log.error("PermissionController.delete.error: {}", e.getMessage(), e);
            return Result.fail("删除权限失败!");
        }
    }
}
